<template>
  <!-- 维护弹框 -->
  <el-dialog title="订单维护" width="680px" :visible.sync="visible" :close-on-click-modal="false" :before-close="beforeClose">
    <el-form ref="form" :model="formData" label-width="100px">
      <el-row :gutter="0">
        <el-col :span="12">
          <el-form-item label="任务编码:">
            {{ formData.modelRoomTaskCode }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务名称:">
            {{ formData.modelRoomTaskName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="提报月份:">
            {{ formData.submissionDate }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="小区名称:">
            {{ formData.communityName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="样板间楼栋号:">
            {{ formData.storiedBuildingNumber }}
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <table-box
      ref="refTable"
      show-selection
      no-request
      :data="tableData"
      :click-row-selection="false"
      :show-custom-setting="false"
      :table-config="settingConfig"
      @selection-change="selectChange"
    >
      <template v-slot:table-before>
        <el-button-group>
          <el-button type="primary" @click="add">增行</el-button>
        </el-button-group>
        <el-button-group>
          <el-button type="primary" @click="del">删行</el-button>
        </el-button-group>
      </template>
      <template>
        <el-table-column slot="before" fixed="left" label="序号" width="50" align="center" type="index" />
        <el-table-column label="销售系列" prop="salesSeriesItem">
          <template slot-scope="{ row }">
            <input-with-dialog
              ref="refSearchType"
              v-model="row.salesSeriesItem"
              :props="{label:'name',value:'code'}"
              :focus-query="true"
              remote-url="/select-service/api/v1/productSaleSeries/page"
              request-method="post"
              :multiple="false"
              :config="{
                title:'销售系列',
                width:'600px',
                interfacePath:'/select-service/api/v1/productSaleSeries/page',
                method:'post',
                filtersRaw:[],
                paramsRaw:{},
                setting:[
                  {prop:'code',title:'编码'},
                  {prop:'name',title:'名称'}
                ]
              }"
            />
          </template>

        </el-table-column>
      </template>
    </table-box>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="beforeClose">取 消</el-button>
      <el-button v-loading="loading" size="small" type="primary" @click="handleOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { orderMaintain } from '@/api/showRoomInfo'
export default {
  props: {
    visible: {
      type: Boolean,
      default() {
        return false
      }
    },
    rows: {
      type: [Array, Object],
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      tableData: [],
      tableSelected: [],
      formData: {},
      rules: {},
      settingConfig: {
        hideSearchBar: true,
        sort: '',
        buttonGroupCode: 'no',
        interfacePath: '',
        setting: [
          {
            prop: 'orderType',
            label: '订单类型',
            overflowHidden: true,
            edit: {
              editType: 'select',
              dataUrl: '/data-dict/api/v2/dictionary/YBJ_ORDER_TYPE',
              paramField: { domainCode: 'marketing', appCode: 'slm' },
              defaultProps: { label: 'dictName', value: 'dictCode' }
            }
          }, {
            prop: 'orderNumber',
            label: '接单编号',
            overflowHidden: true,
            edit: {
              editType: 'text'
            }
          }
        ]
      }
    }
  },
  created() {
    this.formData = this.rows[0]
  },
  methods: {
    add() {
      const newLine = {
        modelRoomInfoId: this.rows[0].id,
        signingRealEstateId: this.rows[0].signingRealEstateId,
        orderType: '',
        orderNumber: '',
        salesSeriesItem: '',
        xplTimer: 'xpl' + new Date().getTime() // 定义timer标识用来操作删除
      }
      this.tableData.unshift(newLine)
    },
    del() {
      if (!this.singleJudge(this.tableSelected, '删除', false)) {
        return
      }
      const movId = []
      for (let i = 0; i < this.tableSelected.length; i++) {
        const item = this.tableSelected[i]
        movId.push(item.xplTimer)
      }
      this.tableData = this.tableData.filter(item => !movId.includes(item.xplTimer))
    },
    selectChange(v) {
      this.tableSelected = v
    },
    async handleOk() {
      if (this.tableData.length < 1) {
        this.$message.warning('请维护信息')
        return
      }

      const tablData = this.tableData

      const keyList = ['orderType', 'orderNumber', 'salesSeriesItem']
      for (let i = 0; i < tablData.length; i++) {
        const item = tablData[i]
        for (let i = 0; i < keyList.length; i++) {
          const key = keyList[i]
          if (Array.isArray(item[key]) && item[key].length < 1) {
            this.$message.warning('请检查信息后确认')
            return false
          }
          if (item[key] === '') {
            this.$message.warning('请检查信息后确认')
            return false
          }
        }
      }
      this.loading = true
      try {
        this.tableData.forEach(item => {
          item.salesSeries = item.salesSeriesItem[0].value
        })
        const res = await orderMaintain(this.tableData)
        if (res.success) {
          this.$message.success('订单维护成功')
          this.$emit('close', true)
        } else {
          this.$message.warning(res.msg)
        }
        this.loading = false
      } catch (err) {
        this.loading = false
      }
    },
    beforeClose() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
